import { Toast } from "vant";
import DList from "@/components/dlist";

export default {
  data() {
    return {
      loading: false,
      page: 1,
      isLoadMore: true,
      communityStoreList: [],

      store_title: "",
      search: "",
      address: "",
      city: "",
      point: {}
    };
  },
  mounted() {
    this.ready();
  },
  activated() {
    if(this.$store.state.referer) {
      this.$store.commit('setReferer', "");
      this.initData();
      this.ready();
    }
  },
  methods: {
    initData() {
      this.loading = false;
      this.page = 1;
      this.isLoadMore = true;
      this.communityStoreList = [];
    },
    chooseCommunityStore(item) { // 跳转社区详情
      this.$router.push(this.fun.getUrl("communityStoreDetails",{fromHome: 1}, {id: item.id}));
    },
    tolocation() {
      this.$store.commit('setReferer',window.location.href);
      this.$router.push(this.fun.getUrl("o2oLocation",{},{tag:'community'}));
    },
    searchStore() {
      this.initData();
      this.getCommunityStoreListByPosition();
    },
    getCommunityStoreListByPosition() { // 获取社区数据
      if (!this.city) {
        Toast('请先选择定位');
        return;
      }
      if (this.loading === true || this.isLoadMore === false) {
        return;
      }
      this.loading = true;
      let urls = "plugin.store-community.frontend.community.getList"; // 请求地址
      $http.get(urls, {
        lng: this.point.lng,
        lat: this.point.lat,
        city_name: this.city,
        page: this.page,
        kwd: this.search
      }, "").then(res => {
        if (res.result === 1) {
          this.store_title = res.data.plugin_name || '社区门店';
          this.fun.setWXTitle(this.store_title);
          this.page++;
          if (res.data.list.current_page === res.data.list.last_page || res.data.list.data.length === 0 || res.data.list.data.length < res.data.list.per_page) {
            this.isLoadMore = false;
          }
          this.communityStoreList = this.communityStoreList.concat(res.data.list.data);
          this.loading = false;
        } else {
          Toast(res.msg);
        }
      }).catch(error => {
        console.error(error);
      });
    },
    enterSearch(event) {
      if (event.keyCode === 13) {
        //如果按的是enter键
        event.preventDefault();
        this.searchStore();
      }
    },
    ready() {
      // let myLocation = this.$store.state.o2oLocation;
      // console.info(myLocation);
      // if (myLocation.point) {
      //   this.address = myLocation.title;
      //   this.point = myLocation.point;
      //   this.city = myLocation.city;
      //   this.getCommunityStoreListByPosition();
      // } else {
      //   console.info("location不存在");
      // }

      this.fun.getLocation()
        .then((res) => {
          this.address = res.title;
          this.point = res.point;
          this.city = res.city;
          this.getCommunityStoreListByPosition();
        })
        .catch((err) => {
          console.log(err);
        });
    }
  },
  components: {
    DList
  }
};
